package com.common.demo.controller;
import com.common.demo.entity.User;
//import com.common.demo.service.service;
import com.common.demo.service.service;
import com.common.demo.util.JwtUtil;
import com.common.demo.util.VfResult;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @Description Logincontroller
 * @Author xiao  xun
 * @Date 2025-09-10
 */
@RestController
@RequestMapping("/login")
public class Logincontroller {
    @Autowired
    private service Service;
    @Autowired
    private JwtUtil jwtUtil;
@Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @GetMapping("/login")
    public VfResult login(@RequestBody User user) {
        User loginUser =Service.select(user.getUsername(), user.getPassword());
        if (loginUser != null) {
            HashMap<String, Object> clime = new HashMap<>();
            clime.put("username", loginUser.getUsername());
            clime.put("role", loginUser.getRole());
            String token = jwtUtil.createJWT("陌", 60 * 60 * 1000, clime);
            //
            String key = loginUser.getUsername();
            List<String> accessUrls =Service.selectrole(loginUser.getUsername());
            redisTemplate.opsForValue().set(key, accessUrls, 60 * 60 * 1000, java.util.concurrent.TimeUnit.MILLISECONDS);
            return VfResult.ok(token);
        }

        return VfResult.errorException("登录失败");
    }
    @GetMapping("/logout")
    public VfResult logout() {
        return VfResult.ok("Hello world");
    }
    @GetMapping("/log")
    public VfResult log() {
        return VfResult.ok("Hello log");
    }
}
